package com.demo.mybatis;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

import com.demo.mybatis.entity.XMLBean;

public class CMapperProxy<T> implements InvocationHandler{
	
	 private CsqlSession sqlSession;

	public CMapperProxy(CsqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}


	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		List<XMLBean> list = CConfigeration.loadMapperXML();
		for (XMLBean xmlBean : list) {
			if(method.getDeclaringClass().getName().equals(xmlBean.getNamespace())){
				String sql = xmlBean.getMed_sql().get(method.getName());
			    System.out.println(String.format("SQL [ %s ], parameter [%s]", sql, args[0]));
			    return sqlSession.selectOne(sql, args[0]);
//			return method.invoke(sqlSession, sql,args[0]);为什么这样写会报错？
			}
		}
		
		return null;
	}

}
